// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.areas.isoregion.regions;

import java.util.ArrayDeque;
import zombie.core.Color;
import zombie.core.Colors;
import zombie.iso.areas.isoregion.IsoRegions;
import zombie.iso.areas.isoregion.data.DataRoot;

public final class IsoRegionManager {
    private final ArrayDeque<IsoWorldRegion> poolIsoWorldRegion = new ArrayDeque();
    private final ArrayDeque<IsoChunkRegion> poolIsoChunkRegion = new ArrayDeque();
    private final DataRoot dataRoot;
    private final ArrayDeque<Integer> regionIdStack = new ArrayDeque();
    private int nextID = 0;
    private int colorIndex = 0;
    private int worldRegionCount = 0;
    private int chunkRegionCount = 0;

    public IsoRegionManager(DataRoot _dataRoot) {
        this.dataRoot = _dataRoot;
    }

    public IsoWorldRegion allocIsoWorldRegion() {
        IsoWorldRegion isoWorldRegion = !this.poolIsoWorldRegion.isEmpty() ? (IsoWorldRegion)this.poolIsoWorldRegion.pop() : new IsoWorldRegion(this);
        int _int = this.regionIdStack.isEmpty() ? this.nextID++ : (Integer)this.regionIdStack.pop();
        isoWorldRegion.init(_int);
        this.worldRegionCount++;
        return isoWorldRegion;
    }

    public void releaseIsoWorldRegion(IsoWorldRegion worldRegion) {
        this.dataRoot.DequeueDirtyIsoWorldRegion(worldRegion);
        if (!worldRegion.isInPool()) {
            this.regionIdStack.push(worldRegion.getID());
            worldRegion.reset();
            this.poolIsoWorldRegion.push(worldRegion);
            this.worldRegionCount--;
        } else {
            IsoRegions.warn("IsoRegionManager -> Trying to release a MasterRegion twice.");
        }
    }

    public IsoChunkRegion allocIsoChunkRegion(int zLayer) {
        IsoChunkRegion isoChunkRegion = !this.poolIsoChunkRegion.isEmpty() ? (IsoChunkRegion)this.poolIsoChunkRegion.pop() : new IsoChunkRegion(this);
        int _int = this.regionIdStack.isEmpty() ? this.nextID++ : (Integer)this.regionIdStack.pop();
        isoChunkRegion.init(_int, zLayer);
        this.chunkRegionCount++;
        return isoChunkRegion;
    }

    public void releaseIsoChunkRegion(IsoChunkRegion chunkRegion) {
        if (!chunkRegion.isInPool()) {
            this.regionIdStack.push(chunkRegion.getID());
            chunkRegion.reset();
            this.poolIsoChunkRegion.push(chunkRegion);
            this.chunkRegionCount--;
        } else {
            IsoRegions.warn("IsoRegionManager -> Trying to release a ChunkRegion twice.");
        }
    }

    public Color getColor() {
        Color color = Colors.GetColorFromIndex(this.colorIndex++);
        if (this.colorIndex >= Colors.GetColorsCount()) {
            this.colorIndex = 0;
        }

        return color;
    }

    public int getWorldRegionCount() {
        return this.worldRegionCount;
    }

    public int getChunkRegionCount() {
        return this.chunkRegionCount;
    }
}
